package com.klhappy.houyuan.utils;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.klhappy.houyuan.R;
import com.klhappy.houyuan.constant.KeyConstant;
import com.klhappy.houyuan.constant.UrlConstant;
import com.klhappy.houyuan.utils.imageselector.Bimp;
import com.klhappy.houyuan.utils.okhttp.BaseBeanCallback;
import com.klhappy.houyuan.utils.okhttp.ImgBean;
import com.klhappy.houyuan.utils.okhttp.builder.PostFormBuilder;
import com.squareup.okhttp.Request;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by MrPu on 16/1/13.
 */
public class EvaluateDeal {
    private Context context;

    private static int picCounter = 0;

    public List<String> name = new ArrayList<>();

    private String rec_id;

    public Dialog mDialog = null;

    private Toast toast;

    public EvaluateDeal(Context context, String rec_id) {
        this.context = context;
        this.rec_id = rec_id;
        showDialog("上传中...");
    }

    public void dealPicture(File[] files) {

        //图片压缩检测
        for (int i = 0; i < files.length; i++) {
            int size = FileUtils.getFileSize(files[i]);
            Log.e("图片处理", "0---size=" + size);//压缩处理前的文件大小
            if (1.0 * size / 1000 / 1000 >= 0.1) {//文件大于100k，压缩处理
                if (!mDialog.isShowing()) {
                    show();
                }
                picCounter++;
                dealWithPic(files, i, size);
            }
        }
        if (picCounter <= 0) {
            setPost(files);
        }
    }

    /**
     * 处理图片
     */
    private void dealWithPic(File[] files, int index, int size) {
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
        String date = sDateFormat.format(new java.util.Date());
        int num = (int) (Math.random() * 10000);
        String fileName = date + num + ".jpg";
        name.add(fileName);
        String path = FileUtils.SDPATH + File.separator + fileName;
        final File newFile = new File(path);
        try {
            if (!newFile.exists()) {
                newFile.createNewFile();
            }
            PicAsynTask task = new PicAsynTask(files, newFile, 700000, size, index);
            task.setPicCompressCallback(new PicAsynTask.PicCompressCallback() {
                @Override
                public void dealOk(File[] files, int index, File newFile) {
                    files[index] = newFile;
                    Log.e("图片处理", "1---size=" + FileUtils.getFileSize(files[index]) + files[index].getAbsolutePath());//压缩处理后的文件大小
                    picCounter--;
                    if (picCounter <= 0) {
                        setPost(files);
                    }
                }
            });
            task.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void setPost(File[] files) {
        String path = UrlConstant.COMONE_URL;
        ConfigManager mConfigManager = new ConfigManager(context);

        Map<String, String> params = new HashMap<>();
        params.put("rec_id", rec_id);
        Map<String, String> headers = new HashMap<>();
        headers.put("cookie", mConfigManager.get(KeyConstant.session, ""));

        PostFormBuilder builder = new PostFormBuilder();
        for (int i = 0; null != files && i < files.length; i++) {
            builder.addFile("header_file", name.get(i), files[i]);
        }
        builder.url(path)
                .params(params)
                .headers(headers)
                .build()
                .execute(new BaseBeanCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        Log.e("this", "ddddd" + e.toString());
                        closeDialog();
                    }

                    @Override
                    public void onResponse(ImgBean imgBean) {
                        Log.e("this", "aaaaa" + imgBean.msg);
                        closeDialog();
                    }
                });
    }

    public void showDialog(String msg) {
        mDialog = new Dialog(context, R.style.progress_dialog);
        mDialog.setContentView(R.layout.dialog);
        mDialog.setCancelable(true);
        mDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        TextView tv_Msg = (TextView) mDialog.findViewById(R.id.id_tv_loadingmsg);
        tv_Msg.setText(msg);
    }

    public void show(){
        mDialog.show();
    }

    public void closeDialog() {
        if(mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }

    public void showToastx(String msg){
        if (toast != null){
            toast.cancel();
        }
        toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
        //可以控制toast显示的位置
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();

    }
}
